﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;

namespace DAO
{
    public class DAO_KhoaHoc
    {
        public static List<DTO_KhoaHoc> SelectKhoaHoc()
        {
            List<DTO_KhoaHoc> listKhoaHoc = new List<DTO_KhoaHoc>();
            try
            {
                string s = "Proc_SelectKhoaHoc";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_KhoaHoc khoahoc = new DTO_KhoaHoc();
                    khoahoc.MaKH = dt.Rows[i]["MaKH"].ToString();
                    khoahoc.NamBD = int.Parse(dt.Rows[i]["NamBD"].ToString());
                    khoahoc.NamKT = int.Parse(dt.Rows[i]["NamKT"].ToString());
                    khoahoc.SoLop = int.Parse(dt.Rows[i]["SoLop"].ToString());
                    khoahoc.MaKhoa = dt.Rows[i]["MaKhoa"].ToString();
                    listKhoaHoc.Add(khoahoc);
                }
                ketnoi.Cmb().Close();
                return listKhoaHoc;
            }
            catch (Exception ex)
            {
                return listKhoaHoc;
            }
            
        }
        public static List<DTO_KhoaHoc> SelectKhoaHoc_theokhoa(string makhoa)
        {
            List<DTO_KhoaHoc> listKhoaHoc = new List<DTO_KhoaHoc>();
            try
            {
                string s = "proc_selectKhoaHoc_TheoKhoa";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@MaKhoa", SqlDbType.NVarChar).Value = makhoa;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_KhoaHoc khoahoc = new DTO_KhoaHoc();
                    khoahoc.MaKH = dt.Rows[i]["MaKH"].ToString();
                    khoahoc.NamBD = int.Parse(dt.Rows[i]["NamBD"].ToString());
                    khoahoc.NamKT = int.Parse(dt.Rows[i]["NamKT"].ToString());
                    khoahoc.SoLop = int.Parse(dt.Rows[i]["SoLop"].ToString());
                    khoahoc.MaKhoa = dt.Rows[i]["MaKhoa"].ToString();
                    listKhoaHoc.Add(khoahoc);
                }
                ketnoi.Cmb().Close();
                return listKhoaHoc;
            }
            catch (Exception ex)
            {
                return listKhoaHoc;
            }

        }

        public static bool deleteKhoaHoc(string makh)
        {
            List<DTO_KhoaHoc> listKhoaHoc = new List<DTO_KhoaHoc>();
            // lay danh sach Khoa hoc theo Ma Khoa
            try
            {
                string s = "Proc_deleteKhoaHoc";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@MaKH", SqlDbType.NVarChar).Value = makh;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable sv = new DataTable();
                da.Fill(sv);
                for (int i = 0; i < sv.Rows.Count; i++)
                {
                    DTO_KhoaHoc khoahoc = new DTO_KhoaHoc();
                    khoahoc.MaKH = sv.Rows[i]["MaKH"].ToString();
                    listKhoaHoc.Add(khoahoc);
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool insertKhoaHoc(DTO_KhoaHoc khoahoc)
        {
           try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_InsertKhoaHoc";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@MaKH", khoahoc.MaKH);
                cmd1.Parameters.AddWithValue("@NamBD", khoahoc.NamBD);
                cmd1.Parameters.AddWithValue("@NamKT", khoahoc.NamKT);
                cmd1.Parameters.AddWithValue("@SoLop", khoahoc.SoLop);
                cmd1.Parameters.AddWithValue("@MaKhoa", khoahoc.MaKhoa);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
               return false;
            }
            return true;
        }

        public static bool updateKhoaHoc(DTO_KhoaHoc khoahoc, string makhoahoc_cu)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_UpdateKhoaHoc";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@MaKH", khoahoc.MaKH);
                cmd1.Parameters.AddWithValue("@NamBD", khoahoc.NamBD);
                cmd1.Parameters.AddWithValue("@NamKT", khoahoc.NamKT);
                cmd1.Parameters.AddWithValue("@SoLop", khoahoc.SoLop);
                cmd1.Parameters.AddWithValue("@MaKhoa", khoahoc.MaKhoa);
                cmd1.Parameters.AddWithValue("@MaKH_Cu", makhoahoc_cu);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
    }
}
   
